29. Optional Exercise: Adding the Buzzer
L5 54 Add Buzzing
Where should it go?
SOLUTION:
ViewModelIs this Event or State?
SOLUTION:
EventNow it’s your turn to complete this exercise yourself.
Let's add a buzzer! Before you start, check the reference documentation on how to use Vibration on Android. This will mostly be a self-guided exercise, but we'll give you a few steps to get started.
1. Add the Vibrate permission:
In the AndroidManifest.xml file, above the application tag, add the following tag:
<uses-permission android:name="android.permission.VIBRATE" />
This provides a permission that lets us vibrate the phone. We will describe Permissions in greater detail later in this course - suffice it to say that without this, our app cannot cause the phone to vibrate on its own.
2. You can also optionally lock the screen to landscape:
While you're in the manifest, you can also optionally lock the phone to landscape mode. That is done by adding the following lines to the MainActivity tag:
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="landscape">
3. Copy over the different buzz pattern Long array constants:
Vibration is controlled by passing in an array representing the number of milliseconds each interval of buzzing and non-buzzing takes. So the array [0, 200, 100, 300] will wait 0 milliseconds, then buzz for 200ms, then wait 100ms, then buzz fo 300ms. Here are some example buzz patterns you can copy over:
private val CORRECT_BUZZ_PATTERN = longArrayOf(100, 100, 100, 100, 100, 100)
private val PANIC_BUZZ_PATTERN = longArrayOf(0, 200)
private val GAME_OVER_BUZZ_PATTERN = longArrayOf(0, 2000)
private val NO_BUZZ_PATTERN = longArrayOf(0)
Put these in the GameViewModel, above the class.
4. Make an enum called BuzzType in GameViewModel
This enum will represent the different types of buzzing that can occur:
enum class BuzzType(val pattern: LongArray) {
CORRECT(CORRECT_BUZZ_PATTERN),
GAME_OVER(GAME_OVER_BUZZ_PATTERN),
COUNTDOWN_PANIC(PANIC_BUZZ_PATTERN),
NO_BUZZ(NO_BUZZ_PATTERN)
}
5. Copy over the buzz method
Given a pattern, this method will actually perform the buzz. It uses the activity to get a system service, so you should put this in your GameFragment:
private fun buzz(pattern: LongArray) {
val buzzer = activity?.getSystemService<Vibrator>()
buzzer?.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
buzzer.vibrate(VibrationEffect.createWaveform(pattern, -1))
} else {
//deprecated in API 26
buzzer.vibrate(pattern, -1)
}
}
}
At this point you should be able to call buzz to make the phone buzz. The rest of the exercise uses the concepts you've learned in this lesson - the ViewModel decides when to do things, and should communicate events and state changes to the fragment using LiveData. Use the appropriate pattern to model events so you don't end up with extra buzzing. Good Luck!
If you want to start at this step, you can download this exercise code from: Step.11-Exercise-Optional-Add-Buzzing.
You will find plenty of //TODO comments to help you complete this exercise, and if you get stuck go back and watch the video again.
Once you’re done, you can check your solution against the solution we’ve provided here Step.11-Solution-Optional-Add-Buzzing or using this git diff.
Task Description:
Check the steps below as you implement them to complete this exercise.
Task Feedback:
Excellent work! You've finished the Guess It app, along with this extra credit work! You can check your solution against the solution we’ve provided here Step.11-Solution-Optional-Add-Buzzing or using this git diff.
Reference documentation